<?php
/**
* @software         [JiJinCMS]
* @author           [上海集锦信息科技有限公司]
* @website          [http://www.jijinweb.com]
* @email            [sunny@jijinweb.com]
**/
namespace app\admin\model;
use think\Model;
use think\facade\Db;
class CatModel extends Model{
    const catCachePath='data/cache/catCache.php';//栏目缓存文件
    protected $name = 'cat';
    protected $pk = 'catid';
    //更新栏目缓存
    public function updateCache(){
        $cat=$this->field("catid,modelid,classpath,pubid,table_name,name,infonum,lencord,status,pid,sort,level,parent_catids,son_catids,listtemp,viewtemp,thumb,pagetitle,keywords,description,listorder,reorder,member_group_id")->select()->toArray();
        $newcat=array();
        foreach($cat as $v){
            //更新栏目信息数
            $where=array();
            $where['catid']=array('in',getSonCat($v["catid"]));
            $count=Db::name("cms_".$v['table_name'])->where($where)->count();
            $this->update(array(
                "catid"=>$v["catid"],
                "infonum"=>$count,
            ));    
            //            
            $newcat[$v["catid"]]=$v;
        }
        $arr_str=var_export ($newcat,true);
        $arr_str="<?php \r\n \$GLOBALS['cat']=".$arr_str.";";        
        file_put_contents(base_path().self::catCachePath,$arr_str);        
    }
    //
    //更新指定栏目的父级
    public function updateParent_catids($catid){
        $parent_catids=$this->getParents($catid);
        $this->update(array(
            "catid"=>$catid,
            "parent_catids"=>$parent_catids,
        ));
        return $parent_catids;
    }
    //更新指定栏目的子极
    public function updateSon_catids($catid){
        $son_catids=$this->getSons($catid);
        $this->update(array(
            "catid"=>$catid,
            "son_catids"=>$son_catids,
        ));
    }   
    //查找一个栏目的所有父级catid
    protected function getParents($catid){
        $catarr=array();
        for($i=1;$i<=2;$i++){
            $cat=$this->field("catid,pid")->find($catid);
            $pid=$cat["pid"];
            if($pid){
                $catarr[]=$pid;
                $catid=$pid;
                $i=1;
            }else{
                break;
            }
        }
        //数组颠倒
        if($catarr){
            $catarr=array_reverse($catarr);
        }
        $catarr=  implode("|",$catarr);
        if($catarr){
            $catarr="|".$catarr."|";
        }
        return $catarr;
    }    
    //查找一个栏目的所有子级catid
    protected function getSons($catid){
        $catidArr=$this->forCat($catid);
        $catidArr=  implode("|",$catidArr);
        if($catidArr){
            $catidArr="|".$catidArr."|";
        }
        return $catidArr;          
    }
    protected function forCat($pid,$catidArr=[]){
        $cat=$this->field("catid")->where("pid",$pid)->order("sort asc")->select();
        if(count($cat)>0){
            foreach($cat as $v){
                $catidArr[]=$v['catid'];
                $catidArr=$this->forCat($v['catid'],$catidArr);
            }            
            return $catidArr;
        }else{
            return $catidArr;
        }      
    }     
    //重新对son_catids中的level进行计算
    public function updateSon_catidsLevel($ids){
        $ids=explode("|",$ids);
        $where[]=["catid","in",$ids];
        $cat=$this->field("catid,parent_catids")->where($where)->select();
        foreach($cat as $v){
            $arr=explode("|",$v['parent_catids']);
            $count=count(array_filter($arr))+1;
            $this->update(array(
                "catid"=>$v['catid'],
                "level"=>$count,
            ));
        }
    }    
}
